/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.saxon.expath;

import java.util.Collection;
import net.sf.saxon.Configuration;
import net.sf.saxon.functions.ExtensionFunctionDefinition;
import net.sf.saxon.trans.XPathException;
import org.expath.pkg.saxon.java.ConfigHelper;
import org.expath.pkg.saxon.java.ConfigHelper.ConfigHelperState;
import org.expath.pkg.saxon.java.EXPathFunctionDefinition;

/**
 *
 * @author georgfl
 */
public class ConfigHelper92
        implements ConfigHelperState
{
    public void registerExtensionFunctions(Configuration config, ConfigHelper helper)
            throws XPathException
    {
        try {
            for ( String name : helper.getExtensionClasses(config) ) {
                // DEBUG: TODO: Make it dependent on an option (like the
                // --verbose level...)
                System.err.println("Register class " + name);
                Class clazz;
                try {
                    clazz = Class.forName(name);
                }
                catch ( ClassNotFoundException ex ) {
                    clazz = Class.forName(name, true, helper.getClassLoader());
                }
                if ( EXPathFunctionDefinition.class.isAssignableFrom(clazz) ) {
                    Class<EXPathFunctionDefinition> c = clazz.asSubclass(EXPathFunctionDefinition.class);
                    EXPathFunctionDefinition fun = c.newInstance();
                    fun.setConfiguration(config);
                    config.registerExtensionFunction(fun);
                }
                else if ( ExtensionFunctionDefinition.class.isAssignableFrom(clazz) ) {
                    Class<ExtensionFunctionDefinition> c = clazz.asSubclass(ExtensionFunctionDefinition.class);
                    ExtensionFunctionDefinition fun = c.newInstance();
                    config.registerExtensionFunction(fun);
                }
                else {
                    throw new XPathException("Not a proper ExtensionFunctionDefinition: " + clazz);
                }
            }
        }
        catch ( ClassNotFoundException ex ) {
            throw new XPathException("Error registering Java extension function", ex);
        }
        catch ( InstantiationException ex ) {
            throw new XPathException("Error registering Java extension function", ex);
        }
        catch ( IllegalAccessException ex ) {
            throw new XPathException("Error registering Java extension function", ex);
        }
    }

    public String getQueryWrapperCatalog(Configuration config)
    {
        return "HE".equals(config.getEditionCode())
                ? "xquery-empty.xml"
                : "xquery-wrappers.xml";
    }

    public String getStyleWrapperCatalog(Configuration config)
    {
        return "HE".equals(config.getEditionCode())
                ? "xslt-empty.xml"
                : "xslt-wrappers.xml";
    }
}
